using System;
using System.Collections.Generic;
using System.Linq;
using EasyGrocer.DataLayer;

namespace EasyGrocer.BusinessLayer.ServiceRepository
{
    public class ProductServiceRepImpl:IProductServiceRep
    {
        private static EasyGrocerDBDataContext _dc;
        public IEnumerable<Product> GetByCategoryId(Guid id)
        {
            _dc = DataContextContainer.CurrentDataContext;
            IEnumerable<Product> products = (from p in _dc.Products where p.CategoryID == id select p).OrderBy(p=>p.Name);
            return products;
        }
        public Product GetByProductId(Guid id)
        {
            _dc = DataContextContainer.CurrentDataContext;
            IEnumerable<Product> products = (from p in _dc.Products where p.ID == id select p);
            if(products.Count()==1)
            {
                return products.First();
            }
            else
            {
                throw new Exception("error retrieving proudct");
            }
        }
    }
}